<template>
	<view class="content">
		<carouselMap></carouselMap>
		<sort></sort>
		<view class="wrap">
			<u-button @click="refreshList">猜你喜欢</u-button>
			<u-waterfall v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>

					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>

					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>

	</view>
</template>

<script>
	import {
		store
	} from '@/uni_modules/uni-id-pages/common/store.js';
	import {
		get,
		post,
		setToken
	} from '@/utils/api.js';
	import carouselMap from '../../components/carouselMap.vue';
	import sort from '../../components/sort.vue';
	import userinfoVue from '../../uni_modules/uni-id-pages/pages/userinfo/userinfo.vue';
	export default {
		components: {
			carouselMap,
			sort,
		},
		onLoad() {
			this.onload();
			this.addRandomData();
			const token = uni.getStorageSync('uni_id_token');
		    if(uni.getStorageSync('payStatus'))
			{
				uni.showToast({
					title: '请前往‘我的订单’支付',
					//将值设置为 success 或者直接不用写icon这个参数
					icon: 'none',
					//显示持续时间为 2秒
					duration: 2000
				})  
			}
			uni.removeStorageSync('payStatus')	
		},
		beforeDestroy() {
		 uni.removeStorageSync('payStatus')	
		},
        //点击输入框跳转搜索页
		onNavigationBarSearchInputClicked(e){
			uni.navigateTo({
				url:'/pages/search/search'
			})
		},
		created() {
			var pages = getCurrentPages() // 获取栈实例
			 let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
			this.currentRoute = currentRoute
			this.countShopNum()

        
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			// 模拟数据加载
			setTimeout(() => {
				this.addRandomData();
				this.loadStatus = 'loadmore';
			}, 1)
		},
		data() {
			return {
				loadStatus: 'loadmore',
				flowList: [
				],
				list: [{}],
				currentRoute:'', //当前页面路径
			};
		},
		methods: {

			gotoPro(productId,productByStore) {
				uni.setStorageSync('productId',productId)
				uni.setStorageSync('productByStore',productByStore)
				uni.navigateTo({
					url:'/pages/proDetail/proDetail'
				})
			},
			countShopNum()
			{
				post('/cart/getCartCountByUserName',
				 {
					 cartUser:store.userInfo.username
				 }
				 , {
				  needToken: true, 	
					success: (res) => {
					if (res.statusCode === 200) {
						uni.setStorageSync('shopNum',res.data.data)		
						console.log(res.data.data)
						}
					}
				}); 
			},
			onload() {
				uni.request({
					url: 'http://10.22.194.87:8181/product/displayProductList',
					method: 'POST',
					success: (res) => {
						if (res.data.code == 300) {
							uni.showToast({
								title: '网络错误',
								icon: 'none'
							})
						} else {
							this.list = res.data;
							this.flowList = res.data;
						}
					},
					fail: (err) => {
						console.error('请求失败', err);
					}
				});
			},
			addRandomData() {
				for (let i = 0; i < 10; i++) {
					let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.list[index]))
					item.id = this.$u.guid();
					this.flowList.push(item);
				}
			},

			refreshList() {
				
			// 检查路由中是否包含 'index',首页则刷新列表
			if (this.currentRoute.includes('index')) {
				// 清空列表
				this.flowList = [];
				// 重新加载数据
				this.addRandomData();
				// 强制刷新当前页面
				uni.reLaunch({
					url: '/pages/index/index',
				});
			}
			},
			onNavigationBarSearchInputClicked() {
				// 监听搜索框点击事件  
				// 跳转到搜索页  
				uni.navigateTo({
					url: '/pages/search/search'
				});

			},
		},
		onPullDownRefresh() {
			//监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
			setTimeout(function() {
				uni.stopPullDownRefresh();
				uni.showToast({
					title: '刷新成功',
					icon: 'none',
					duration: 2000
				});
				uni.reLaunch({
					url: '/pages/index/index'
				})
			}, 1000);
		},
	}
</script>

<style lang="scss">
	body,
	.content {
		background-color: #dea01f;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}
</style>
<style lang="scss">
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>